package com.pfzy.wechatpay.dialog

import android.content.Context
import android.graphics.drawable.ColorDrawable
import android.os.Bundle
import android.util.DisplayMetrics
import android.util.Log
import android.view.*
import android.widget.TextView
import androidx.fragment.app.DialogFragment
import com.pfzy.wechatpay.R

/**
 * Used 分享底部对话框【按常理来讲，应该extends BottomSheetDialog】
 * 但是，因为这个分享对话框有可能需要请求接口，所以最终还是继承DialogFragment
 */
class ShareDialog : DialogFragment() {
    /**View实例 */
    private var myView: View? = null

    /**context实例 */
    private var mContext: Context? = null

    /**标记：用来代表是从哪个界面打开的这个对话框 */
    private var mTag: String? = null

    /**QQ分享 */
    private var shareQQ: TextView? = null

    /**QQ空间分享 */
    private var shareQZone: TextView? = null

    /**微信分享 */
    private var shareWX: TextView? = null

    /**朋友圈分享 */
    private var shareWXCircle: TextView? = null

    /**新浪微博分享 */
    private var shareSina: TextView? = null

    override fun onCreateView(
        inflater: LayoutInflater, container: ViewGroup?,
        savedInstanceState: Bundle?
    ): View? {
        dialog!!.window!!.setBackgroundDrawable(ColorDrawable(0)) //设置背景为透明，并且没有标题
        dialog!!.requestWindowFeature(Window.FEATURE_NO_TITLE) // 去掉标题栏，否则点击正上方一定高度的范围内不会消失
        myView = inflater.inflate(R.layout.dialog_share, container, false)
        return myView
    }

    override fun onActivityCreated(savedInstanceState: Bundle?) {
        // TODO Auto-generated method stub
        super.onActivityCreated(savedInstanceState)
        //初始化控件以及设置
        initView()
        //初始化数据
        initDatas()
        //初始化事件
        initEvents()
    }

    /**
     * 设置宽度和高度值，以及打开的动画效果
     */
    override fun onStart() {
        super.onStart()
        //设置对话框的宽高，必须在onStart中
        val metrics = DisplayMetrics()
        this.getActivity()!!.getWindowManager()!!.getDefaultDisplay().getMetrics(metrics)
        val window: Window? = this.getDialog()!!.getWindow()
        window!!.setLayout(metrics.widthPixels, this.getDialog()!!.getWindow()!!.getAttributes().height)
        window!!.setGravity(Gravity.BOTTOM) //设置在底部
        //打开的动画效果
        //设置dialog的 进出 动画
        getDialog()!!.getWindow()!!.setWindowAnimations(R.style.sharebottomsheetdialog_animation)
    }

    /**实例化控件 */
    private fun initView() {
        shareQQ = myView!!.findViewById<View>(R.id.share_qq) as TextView
        shareQZone = myView!!.findViewById<View>(R.id.share_qzone) as TextView
        shareWX = myView!!.findViewById<View>(R.id.share_weixin) as TextView
        shareWXCircle = myView!!.findViewById<View>(R.id.share_wxcircle) as TextView
        shareSina = myView!!.findViewById<View>(R.id.share_sina) as TextView
    }

    /**
     * 初始化数据：tag标记、标题
     */
    private fun initDatas() {
        mTag = this.getTag()
        Log.e(TAG, "mTag=$mTag")
    }

    /**
     * 初始化监听事件
     */
    private fun initEvents() {

        //QQ分享
        shareQQ!!.setOnClickListener {
            dismiss()
            if (mOnShareClickLitener != null) {
                mOnShareClickLitener!!.onShareToQQ()
            }
        }

        //QQ空间分享
        shareQZone!!.setOnClickListener {
            dismiss()
            if (mOnShareClickLitener != null) {
                mOnShareClickLitener!!.onShareToQZone()
            }
        }

        //微信分享
        shareWX!!.setOnClickListener {
            dismiss()
            if (mOnShareClickLitener != null) {
                mOnShareClickLitener!!.onShareToWX()
            }
        }

        //朋友圈分享
        shareWXCircle!!.setOnClickListener {
            dismiss()
            if (mOnShareClickLitener != null) {
                mOnShareClickLitener!!.onShareToWXCircle()
            }
        }

        //新浪微博分享
        shareSina!!.setOnClickListener {
            dismiss()
            if (mOnShareClickLitener != null) {
                mOnShareClickLitener!!.onShareToSina()
            }
        }
    }

    /*=====================添加OnShareClickLitener回调================================*/
    interface OnShareClickLitener {
        fun onShareToQQ()
        fun onShareToQZone()
        fun onShareToWX()
        fun onShareToWXCircle()
        fun onShareToSina()
    }

    private var mOnShareClickLitener: OnShareClickLitener? = null
    fun setOnShareClickLitener(mOnShareClickLitener: OnShareClickLitener?) {
        this.mOnShareClickLitener = mOnShareClickLitener
    }

    companion object {
        private val TAG = ShareDialog::class.java.simpleName
        fun getInstance(mContext: Context?, bundle: Bundle?): ShareDialog {
            val shareDialog = ShareDialog()
            shareDialog.mContext = mContext
            return shareDialog
        }
    }
}
